Trying to send a specific audio file to a detail view in iOS - iphone

I'm fairly new to iOS programming so pardon me if this is an easy issue to fix.
I'm building musical reference app that has a tableview that drills down to a detail. Through the table, when you select a row it sends the detail a picture and text for a label and a scrollview. I'm trying to make it so that I have a custom button on top of the picture and you will be able to tap that picture to play an audio example.
I've figured out how to get the button to play a sound but I can't figure out how to choose which audio file to play.
Here is the seque where I send the Arrays
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowBasicsDetails"]) {
BasicsDetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
detailViewController.basicsDetailModel = [[NSArray alloc]initWithObjects:
[self.basicsNames objectAtIndex:[myIndexPath row]],
[self.basicsImages objectAtIndex:[myIndexPath row]],
[self.basicsDetails objectAtIndex:[myIndexPath row]],
[self.basicsAudios objectAtIndex:[myIndexPath row]],nil];
}
Here is the viewDidLoad from my detail view.
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
self.basicsLabel.text = [self.basicsDetailModel objectAtIndex:0];
self.basicsImage.image = [UIImage imageNamed:[self.basicsDetailModel objectAtIndex:1]];
self.basicsDetail.text = [self.basicsDetailModel objectAtIndex:2];
I'm planning on using AVAudioPlayer but all I can find on that pulls in a specific audio file like in this example from techotopia
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"Kalimba"
ofType:#"mp3"]];

Related

How to use AVPlayer in multiple view and resume controls?

i create an app with xcode 4.6 on iOS 6.1 with a TabBar and 2 different tableView.
Each table view read a line form a file.plist and when tap on a row, you load a DetailView.
The DetailViewController is the same for the 2 TableView but:
If start app tap on a first button i see FirstView with my table and Audio List, if I choose one file on list I load a Detail view and listener my file very well, but if now tap on second view on TabBar i can start playing other audio file from the second view but i listen overlay to firs audio and AVplayer not override.
There my coding:
From TableView loading plist:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([#"detailList" isEqualToString:segue.identifier]) {
NSIndexPath *index = [self.tableView indexPathForCell:sender];
[[segue destinationViewController] setSaved:[loadData objectAtIndex:index.row]];
}
}
In a detali view:
if (mysound.rate == 1.0) {
UIImage *img = [UIImage imageNamed:#"Play_BT.png"];
[control setImage:img forState:UIControlStateNormal];
[mysound pause];
} else {
UIImage *img = [UIImage imageNamed:#"Pause_BT.png"];
[control setImage:img forState:UIControlStateNormal];
NSURL *url = [NSURL URLWithString:[saved objectForKey:#"audio"]];
mysound = [[AVPlayer alloc] initWithURL:url];
[mysound setAllowsExternalPlayback:YES];
[mysound setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[mysound play];
}
}
Here incoming the issues, this code working very goog, but if tapo on tab bar and load other audio from other plist I listen Audio overlay.
How can use AVPlayer in Override the NSURL *url = [NSURL URLWithString:[saved objectForKey:#"audio"]];when change from tabbar?
Thanks.
Solved, using this code:
In my AppDelegate .h adding
#interface AppDelegate : UIResponder <UIApplicationDelegate> {
AVPlayer *MyPlayer;
}
#property (strong, nonatomic) AVPlayer *MyPlayer;
in my AppDelegate .m inside didFinishLaunchingWithOptions adding
[MySound superclass];
Now can calling a method in all app with this code directly inside a .m file:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSURL *url = [NSURL URLWithString:[saved objectForKey:#"link"]];
appDelegate.MySound = [[AVPlayer alloc] initWithURL:url];
[appDelegate.MySound setAllowsExternalPlayback:YES];
[appDelegate.MySound setUsesExternalPlaybackWhileExternalScreenIsActive: YES];
[appDelegate.MySound play];
With this method now is possible controlling the AVPlayer evry where in the APP!!
I Hope this Help any guys ;)

Not able to get the data in a webview in detailview of splitviewcontroller

In my ipad app i'm not able to get the data in a web view. That web view is in a detail view of splitviewcontroller. That data is coming from a link.
I'm using this code.
In split view table did select row at index path
post=[self.postListMutableArray objectAtIndex:indexPath.row];
PostDetailsViewController *postDetails=[[PostDetailsViewController alloc]initWithNibName:#"PostDetailViewController_Ipad" bundle:nil];
postDetails.detailItem=post.postLink;
In split view detail view
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem)
{
_detailItem = newDetailItem;
// Update the view.
NSLog(#"detail Item String is %#",_detailItem);
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
NSString* str =_detailItem;
NSURL* url = [NSURL URLWithString:str];
NSLog(#"URL is ----> %#",url);
self.webView.scalesPageToFit=YES;
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
//[self.view addSubview:webView];
}
create new Variable at PostDetailsViewController .h class:-
#property(strong,nonatomic)NSString *strGetPostDetail;
and synthesize PostDetailsViewController .m
#synthesize strGetPostDetail
Now you can call splitViewTable Delegate DidSelect method:-
post=[self.postListMutableArray objectAtIndex:indexPath.row];
PostDetailsViewController *postDetails=[[PostDetailsViewController alloc]init];
postDetails.strGetPostDetail=post;

Press a Button and open a URL in another ViewController

I am trying to learn Xcode by making a simple app.
But I been looking on the net for hours (days) and I cant figure it out how I make a button that open a UIWebView in another ViewController :S
first let me show you some code that I have ready:
I have a few Buttons om my main Storyboard that each are title some country codes like UK, CA and DK.
When I press one of those Buttons I have an IBAction like this:
- (IBAction)ButtonPressed:(UIButton *)sender {
// Google button pressed
NSURL* allURLS;
if([sender.titleLabel.text isEqualToString:#"DK"]) {
// Create URL obj
allURLS = [NSURL URLWithString:#"http://google.dk"];
}else if([sender.titleLabel.text isEqualToString:#"US"])
{
allURLS = [NSURL URLWithString:#"http://google.com"];
}else if([sender.titleLabel.text isEqualToString:#"CA"])
{
allURLS = [NSURL URLWithString:#"http://google.ca"];
}
NSURLRequest* req = [NSURLRequest requestWithURL:allURLS];
[myWebView loadRequest:req];
}
How do I make this open UIWebview on my other Viewcontroller named myWebView?
please help a lost man :D
Well, you've firstViewController already designed and coded, now you've to create secondViewController with a UIWebView binded in IB it self, also it have a setter NSString variable like strUrl that you need to pass at the time of pushing or presenting secondViewController, and assign it to UIWebView in viewDidLoad of secondViewController. See my answer about how to pass a NSString? Also UIWebView has its delegates method (What's delegate & datasource methods? - Apple Doc) Which you can use to handle URL request.
These are the delegate of UIWebView, if you'll going to use it, you need to give UIWebView delegate to self.
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
firstViewController.m
- (IBAction)ButtonPressed:(UIButton *)sender {
NSURL* allURLS;
//get your URL
secondViewControlelr *secondView=[[secondViewControlelr alloc]init];
second.urlToLoad=allURLS;
[self.navigationController pushViewController:secondView animated:YES];
}
secondViewControlelr.h
//declare url and set property
NSURL *urlToLoad;
secondViewControlelr.m
- (void)viewDidLoad
{
myWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:myWebView];
NSURLRequest *urlReq=[NSURLRequest requestWithURL:self.urlToLoad cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
[myWebView loadRequest:urlReq];
}

iPhone Creating a Modal Movie Window

I have created an app that incorporates a table view, detail view, and a web view. I have established a cell in the table view that takes a URL from an RSS feed and will display it in a webview. The URL is an .MP4 file which causes the video to play. The problem I am having is that when the video ends, I cannot go back to the previous screen. The code is below:
DetailViewController.m load webview
if (indexPath.section == SectionHeader && indexPath.row == SectionHeaderEnclosure) {
if (item.enclosures) {
for (NSDictionary *dict in item.enclosures){
NSString *url = [dict objectForKey:#"url"];
NSLog(#" url is : %#",url);
//EXPERIMENTAL
WebViewController *webVC = [[WebViewController alloc] initWithURL:url];
[self presentModalViewController:webVC animated:YES];
}
}
}
WebViewController.m
- (void)loadView
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = webView;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
}
//Dismiss modal view
- (IBAction) done:(id)sender {
NSLog(#"done:");
[self dismissModalViewControllerAnimated:YES];
}
I think it would be better to use an MPMoviePlayerViewController. The documentation shows how to set it up with the URL directly using initWithContentURL: and present it modally. You can listen for notifications thet the movie has ended and dismiss it when done. Lots of good stuff in the documentation.

how to play different audio files with same media player in a separate view- iphone sdk?

I've been trying and researching for countless hours but for some reason I just cant get my head around it and I know its a simple answer. Please Help!
I have a view with different buttons, -(ScrollViewController1)
the different buttons push in one view, -(b1ViewController)
this view contains the media player,
In a nutshell, I cant figure out how to get the media player to play a different audio file depending on which button in the first view was pressed.
I'm trying not to make a different view with a different media player for every button.
Could someone demonstrate it please so that I can understand it? Thanks a million.
Heres the relevant code-
(ps- theres a reason for the tags, I didnt want to get too much into for the sake of asking a clear question and keeping it straight forward.)
#implementation ScrollViewController1
- (IBAction)pressedb1view {
if (thebutton.tag==101) {
[self performSelector:#selector(dismissb1pop:) withObject:nil afterDelay:0.1];
b1view.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:b1view animated:YES];
}
if (thebutton.tag==102) {
[self performSelector:#selector(dismissb1pop:) withObject:nil afterDelay:0.1];
b1view.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:b1view animated:YES];
}
if (thebutton.tag==103) {
[self performSelector:#selector(dismissb1pop:) withObject:nil afterDelay:0.1];
b1view.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:b1view animated:YES];
}
}
etc, etc, etc....
in the next view which contains the mediaplayer I have-
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface b1ViewController : UIViewController {
MPMoviePlayerController *moviePlayer;
NSURL *movieURL;
}
in the .m -
-(void)viewWillAppear:(BOOL)animated {
[[self navigationController] setNavigationBarHidden:NO animated:YES];
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"music1" ofType:#"mp3"];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 320, 40);
[moviePlayer play];
}
EDIT:
Im passing text from the first view to a uilabel on the second view using the following in the first view-
[b1view changeSiteText:#"im stuck here"];
in the second view Im doing this in the .h-
- (IBAction) changeSiteText:(NSString *)str;
and in the .m im doing this-
- (IBAction) changeSiteText:(NSString *)str{
lblSiteTxt.text = str;
}
Is there a way to implement a similar method for the mediaplayer,??? maybe doing
something like
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"%#" ofType:#"mp3", str];
Im stuck!!
Help!
Do you push the view or present it?
Add a method in the player class which when passed various arguments it plays the appropriate track or whatever. Then the different IBActions will invoke this method.