IOS media collector not showing

I am currently working on a sound application on an iPhone. It is based on the Apple SpeakHere example with a custom input file from the iPod library.

Here is the event raised by the button:

- (IBAction) btn_PickSong_Clicked:(id)sender{ [self showMediaPicker]; //code importing tracks from library } 

And in the showMediaPicker method:

 //Yup the program does reach this method but the picker does not show up - (void)showMediaPicker { MPMediaPickerController* mediaPicker = [[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic] autorelease]; mediaPicker.delegate = self; [self presentModalViewController:mediaPicker animated:YES]; } 

Problems:

  • The library import function works fine in a separate program, but the media collector no longer appears when I put the code in SpeakHereController.mm .

  • Also, if I put the showMediaPicker method in another class and call it, it also does not work.

Something I find may make a difference:

  • The source code is in the obj-C file (xxx.m), and now it is transferred to the obj-C ++ file (xxx.mm).

  • I also changed the base class SpeakHereController.h from NSObject to UIViewController<MPMediaPickerControllerDelegate> , otherwise it will warn that the base class does not contain the required delegate. But in the interface builder, it is still displayed as an object (see SpeakHere sample code).

  • It seems that it is illegal to convert the embedded xxxViewController.m file to an obj-C ++ file (.mm extension). In this case, many errors appear if I try to do this. It's true? If so, how do I include C ++ code in a clean obj-C file?

==============

So how can I make a media collector in this case? Any insight would be appreciated.

Thank you very much!

Greetings

Manca

+4
source share
1 answer

For

 [self presentModalViewController:mediaPicker animated:YES]; 

To work, I have to be a viewcontroller. I'm worried that you just changed the base class to avoid compiler errors, as this means that you are not really creating the self.

So how do you initialize the SpeakHereController? As a view controller, this will usually be through the designated initializer, which for the UIViewController, of course, is initWithNibName: bundle:

You can find the documentation for the UIViewController .

Regarding the C ++ issue. Although you can mix objective-c and C ++ as you suggest, I would recommend that you encapsulate your C ++ code in your own class, rather than sprinkling it around your viewcontroller code. This will make it more convenient for the future.

+1
source

Source: https://habr.com/ru/post/1342610/


All Articles