質問編集履歴

2

コードが長すぎるので一部削除しました。

2020/03/08 23:19

投稿

reishisu
reishisu

スコア39

test CHANGED
File without changes
test CHANGED
@@ -60,18 +60,6 @@
60
60
 
61
61
  let songId: NSNumber = albums[indexPath.row].songId
62
62
 
63
-
64
-
65
- self.title = albums[indexPath.row].songTitle
66
-
67
-
68
-
69
- // リストビューの破棄
70
-
71
- self.dismiss(animated: true, completion: nil)
72
-
73
-
74
-
75
63
  print("expotItemを呼び出します")
76
64
 
77
65
  instanceOfObjcMethod.exportItem(toId: songId.description)
@@ -140,6 +128,8 @@
140
128
 
141
129
  MusicLibraryMediaPicker.mm
142
130
 
131
+ 全文だと長いので、関係ない部分を削除しております。
132
+
143
133
  ```Objective-C++
144
134
 
145
135
 

1

ソースコードの関係ない部分を削除

2020/03/08 23:19

投稿

reishisu
reishisu

スコア39

test CHANGED
File without changes
test CHANGED
@@ -28,6 +28,8 @@
28
28
 
29
29
  ViewController.swift
30
30
 
31
+ 全文だと長いので、関係ない部分を削除しております。
32
+
31
33
  ```Objective-C++
32
34
 
33
35
  //import Foundation
@@ -44,354 +46,180 @@
44
46
 
45
47
 
46
48
 
47
- var myTableView: UITableView!
48
-
49
- var albums: [SongInfo] = []
50
-
51
- var songQuery: SongQuery = SongQuery()
52
-
53
49
  let instanceOfObjcMethod: ObjcMethod = ObjcMethod()
54
50
 
55
- var audio: AVAudioPlayer! = nil
56
-
57
51
 
58
52
 
59
- weak var delegate: audiosend?
60
-
61
-
62
-
63
- override func viewDidLoad() {
64
-
65
-
66
-
67
- super.viewDidLoad()
68
-
69
-
70
-
71
- albums = songQuery.get()
72
-
73
- if(albums.count == 0){
74
-
75
- self.dismiss(animated: true, completion: nil)
76
-
77
- return;
53
+ // 選択した音楽を再生
54
+
55
+ func tableView( _ tableView: UITableView, didSelectRowAt indexPath:IndexPath ) {
56
+
57
+
58
+
59
+ // soundIdからMediaItemを取得
60
+
61
+ let songId: NSNumber = albums[indexPath.row].songId
62
+
63
+
64
+
65
+ self.title = albums[indexPath.row].songTitle
66
+
67
+
68
+
69
+ // リストビューの破棄
70
+
71
+ self.dismiss(animated: true, completion: nil)
72
+
73
+
74
+
75
+ print("expotItemを呼び出します")
76
+
77
+ instanceOfObjcMethod.exportItem(toId: songId.description)
78
+
79
+ print("expotItemを呼び出し終わりました")
80
+
81
+ }
82
+
83
+ }
84
+
85
+
86
+
87
+ ```
88
+
89
+
90
+
91
+ ObjcSwift.h
92
+
93
+ ```Objective-C++
94
+
95
+ #import <Foundation/Foundation.h>
96
+
97
+
98
+
99
+ #ifndef ObjcSwift_h
100
+
101
+ #define ObjcSwift_h
102
+
103
+
104
+
105
+ @interface ObjcMethod : NSObject
106
+
107
+
108
+
109
+ - (BOOL) exportItemToId: (NSString*) songId;
110
+
111
+ - (void) setDoDisplay: () _ ;
112
+
113
+
114
+
115
+ @end
116
+
117
+
118
+
119
+ #endif /* ObjcSwift_h */
120
+
121
+ ```
122
+
123
+
124
+
125
+ Unity-iPhone-Bridging-Header.h
126
+
127
+ ```Objective-C++
128
+
129
+ #import <Foundation/Foundation.h>
130
+
131
+ #import <UIKit/UIKit.h>
132
+
133
+ #import "UnityInterface.h"
134
+
135
+
136
+
137
+ #import "ObjcSwift.h"
138
+
139
+ ```
140
+
141
+ MusicLibraryMediaPicker.mm
142
+
143
+ ```Objective-C++
144
+
145
+
146
+
147
+ # import "ObjcSwift.h"
148
+
149
+
150
+
151
+ extern "C" {
152
+
153
+ BOOL exportItem (MPMediaItem *item);
154
+
155
+
156
+
157
+ /********************************
158
+
159
+ * クラスを作って処理している
160
+
161
+ ********************************/
162
+
163
+ @implementation ObjcMethod: NSObject
164
+
165
+
166
+
167
+ /**************************************
168
+
169
+ * 曲IDをもとにエクスポートする関数
170
+
171
+ * @param songId エクスポートしたい曲ID
172
+
173
+ * @return エクスポートが完了したらYESを返す
174
+
175
+ **************************************/
176
+
177
+ - (BOOL) exportItemToId: (NSString*) songId {
178
+
179
+ NSLog(@"!!!!!!!!exportItemToId呼ばれたよ!!!!!!!");
180
+
181
+ // char*型の文字列を変換
182
+
183
+ NSString* s_id = [NSString stringWithFormat:@"%@",songId];
184
+
185
+ MPMediaQuery* songQuery = [MPMediaQuery songsQuery];
186
+
187
+ [songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO] forProperty:MPMediaItemPropertyIsCloudItem]];
188
+
189
+ NSArray *songlists = songQuery.collections;
190
+
191
+ for (MPMediaItemCollection *songlist in songlists) {
192
+
193
+ MPMediaItem *representativeItem = [songlist representativeItem];
194
+
195
+ NSString* PersistentID = [NSString stringWithFormat:@"%@", [representativeItem valueForProperty:MPMediaItemPropertyPersistentID]];
196
+
197
+ if ( ![representativeItem hasProtectedAsset] && [s_id isEqualToString:PersistentID] ) {
198
+
199
+ sid = [[representativeItem valueForProperty:MPMediaItemPropertyPersistentID] longValue];
200
+
201
+ sname = [representativeItem valueForProperty:MPMediaItemPropertyTitle];
202
+
203
+ albumArtwork = [representativeItem.artwork imageWithSize:representativeItem.artwork.bounds.size];
204
+
205
+ NSLog(@"見つかりました!");
206
+
207
+ NSLog(@"セットします!![sid:%ld, sname:%@]:", sid, sname);
208
+
209
+ return YES;
210
+
211
+ }
212
+
213
+ }
214
+
215
+ return NO;
78
216
 
79
217
  }
80
218
 
81
-
82
-
83
- let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
84
-
85
- let displayWidth: CGFloat = self.view.frame.width
86
-
87
- let displayHeight: CGFloat = self.view.frame.height
88
-
89
-
90
-
91
- let leftButton = UIBarButtonItem(title: "戻る", style: UIBarButtonItem.Style.plain, target: self, action: #selector(goTop))
92
-
93
- self.navigationItem.leftBarButtonItem = leftButton
94
-
95
-
96
-
97
- myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight))
98
-
99
-
100
-
101
- self.title = "Songs"
102
-
103
-
104
-
105
- self.myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
106
-
107
- self.myTableView.delegate = self
108
-
109
- self.myTableView.dataSource = self
110
-
111
- self.view.addSubview(self.myTableView)
112
-
113
- print(">>> viewDidLoad")
114
-
115
- }
116
-
117
-
118
-
119
- // 選曲時のリストビューを破棄(戻るボタン用)
120
-
121
- @objc func goTop() {
122
-
123
- self.dismiss(animated: true, completion: nil)
124
-
125
- instanceOfObjcMethod.setDoDisplay(_: nil)
126
-
127
- }
128
-
129
-
130
-
131
- // メモリピンチの時
132
-
133
- override func didReceiveMemoryWarning() {
134
-
135
- super.didReceiveMemoryWarning()
136
-
137
- }
138
-
139
- // sectionの数を返す
140
-
141
- func numberOfSectionsInTableView( in tableView: UITableView! ) -> Int {
142
-
143
- return albums.count
144
-
145
- }
146
-
147
-
148
-
149
- // 各sectionのitem数を返す
150
-
151
- func tableView( _ tableView: UITableView, numberOfRowsInSection section: Int ) -> Int {
152
-
153
- return albums.count
154
-
155
- }
156
-
157
-
158
-
159
- func tableView( _ tableView: UITableView, cellForRowAt indexPath : IndexPath) -> UITableViewCell {
160
-
161
- let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
162
-
163
- cell.textLabel!.text = albums[indexPath.row].songTitle
164
-
165
- // 曲検索
166
-
167
- let property = MPMediaPropertyPredicate(value: albums[indexPath.row].songId, forProperty: MPMediaItemPropertyPersistentID)
168
-
169
- let query = MPMediaQuery()
170
-
171
- query.addFilterPredicate(property)
172
-
173
- let single = query.items!
174
-
175
- // セルに画像を入れる
176
-
177
- if let image = single.first!.artwork?.image(at: CGSize(width: 40, height: 40)) {
178
-
179
- cell.imageView!.image = image
180
-
181
- } else {
219
+ @end
182
-
183
- let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
184
-
185
- let imagePath = "(documentPath)/default_artwork.jpg"
186
-
187
- if let data = FileManager.default.contents(atPath: imagePath) { cell.imageView?.image = UIImage(data: data) }
188
-
189
- }
190
-
191
- return cell;
192
-
193
- }
194
-
195
-
196
-
197
- // sectionのタイトル
198
-
199
- func tableView( _ tableView: UITableView, titleForHeaderInSection section: Int ) -> String? {
200
-
201
- return ""
202
-
203
- }
204
-
205
-
206
-
207
- // 選択した音楽を再生
208
-
209
- func tableView( _ tableView: UITableView, didSelectRowAt indexPath:IndexPath ) {
210
-
211
-
212
-
213
- // soundIdからMediaItemを取得
214
-
215
- let songId: NSNumber = albums[indexPath.row].songId
216
-
217
-
218
-
219
- self.title = albums[indexPath.row].songTitle
220
-
221
-
222
-
223
- // リストビューの破棄
224
-
225
- self.dismiss(animated: true, completion: nil)
226
-
227
-
228
-
229
- print("expotItemを呼び出します")
230
-
231
- instanceOfObjcMethod.exportItem(toId: songId.description)
232
-
233
- print("expotItemを呼び出し終わりました")
234
-
235
- }
236
220
 
237
221
  }
238
222
 
239
-
240
-
241
- ```
242
-
243
-
244
-
245
- ObjcSwift.h
246
-
247
- ```Objective-C++
248
-
249
- #import <Foundation/Foundation.h>
250
-
251
-
252
-
253
- #ifndef ObjcSwift_h
254
-
255
- #define ObjcSwift_h
256
-
257
-
258
-
259
- @interface ObjcMethod : NSObject
260
-
261
-
262
-
263
- - (BOOL) exportItemToId: (NSString*) songId;
264
-
265
- - (void) setDoDisplay: () _ ;
266
-
267
-
268
-
269
- @end
270
-
271
-
272
-
273
- #endif /* ObjcSwift_h */
274
-
275
- ```
276
-
277
-
278
-
279
- Unity-iPhone-Bridging-Header.h
280
-
281
- ```Objective-C++
282
-
283
- #import <Foundation/Foundation.h>
284
-
285
- #import <UIKit/UIKit.h>
286
-
287
- #import "UnityInterface.h"
288
-
289
-
290
-
291
- #import "ObjcSwift.h"
292
-
293
- ```
294
-
295
- MusicLibraryMediaPicker.mm
296
-
297
- ```Objective-C++
298
-
299
- # import <Foundation/Foundation.h>
300
-
301
- # import <MediaPlayer/MediaPlayer.h>
302
-
303
- # import <AVFoundation/AVAudioFile.h>
304
-
305
- # import <AVFoundation/AVAudioEngine.h>
306
-
307
- # import <AVFoundation/AVFoundation.h>
308
-
309
- # import <AVFoundation/AVAssetReader.h>
310
-
311
- # import <AVFoundation/AVAssetWriter.h>
312
-
313
- # import <QuartzCore/QuartzCore.h>
314
-
315
- # import <AudioToolBox/AudioToolBox.h>
316
-
317
-
318
-
319
- # import "ObjcSwift.h"
320
-
321
-
322
-
323
- extern "C" {
324
-
325
- BOOL exportItem (MPMediaItem *item);
326
-
327
-
328
-
329
- /********************************
330
-
331
- * クラスを作って処理している
332
-
333
- ********************************/
334
-
335
- @implementation ObjcMethod: NSObject
336
-
337
-
338
-
339
- /**************************************
340
-
341
- * 曲IDをもとにエクスポートする関数
342
-
343
- * @param songId エクスポートしたい曲ID
344
-
345
- * @return エクスポートが完了したらYESを返す
346
-
347
- **************************************/
348
-
349
- - (BOOL) exportItemToId: (NSString*) songId {
350
-
351
- NSLog(@"!!!!!!!!exportItemToId呼ばれたよ!!!!!!!");
352
-
353
- // char*型の文字列を変換
354
-
355
- NSString* s_id = [NSString stringWithFormat:@"%@",songId];
356
-
357
- MPMediaQuery* songQuery = [MPMediaQuery songsQuery];
358
-
359
- [songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithBool:NO] forProperty:MPMediaItemPropertyIsCloudItem]];
360
-
361
- NSArray *songlists = songQuery.collections;
362
-
363
- for (MPMediaItemCollection *songlist in songlists) {
364
-
365
- MPMediaItem *representativeItem = [songlist representativeItem];
366
-
367
- NSString* PersistentID = [NSString stringWithFormat:@"%@", [representativeItem valueForProperty:MPMediaItemPropertyPersistentID]];
368
-
369
- if ( ![representativeItem hasProtectedAsset] && [s_id isEqualToString:PersistentID] ) {
370
-
371
- sid = [[representativeItem valueForProperty:MPMediaItemPropertyPersistentID] longValue];
372
-
373
- sname = [representativeItem valueForProperty:MPMediaItemPropertyTitle];
374
-
375
- albumArtwork = [representativeItem.artwork imageWithSize:representativeItem.artwork.bounds.size];
376
-
377
- NSLog(@"見つかりました!");
378
-
379
- NSLog(@"セットします!![sid:%ld, sname:%@]:", sid, sname);
380
-
381
- return YES;
382
-
383
- }
384
-
385
- }
386
-
387
- return NO;
388
-
389
- }
390
-
391
- @end
392
-
393
- }
394
-
395
223
  ```
396
224
 
397
225