简体   繁体   English

在iPhone上播放音频文件

[英]Playing audio files on the iPhone

I've made a simple app, where I have a list of songs. 我做了一个简单的应用,里面有歌曲列表。 The user taps a list entry and the song begins playing. 用户点击列表条目,然后开始播放歌曲。

I've lifted the SoundEffect class from Apple's sample projects (eg Metronome, BubbleLevel). 我从Apple的示例项目(例如Metronome,BubbleLevel)中取消了SoundEffect类。 It seems to work fine with the following code: 使用以下代码似乎可以正常工作:

// declare in the .h file
SoundEffect *audio;

// setup - when controller loads
audio = [SoundEffect alloc];

// play when user taps entry
NSBundle *mainBundle = [NSBundle mainBundle];   
[audio initWithContentsOfFile:[mainBundle pathForResource:@"entry1" ofType:@"mp3"]];
[audio play];

However, if the 'audio' object is already playing, I'd like to stop it before it starts playing the sound again. 但是,如果“音频”对象已经在播放,我想先停止它,然后再开始播放声音。 SoundEffect class does not have a stop method or I am simply missing something. SoundEffect类没有stop方法,或者我只是丢失了一些东西。

How do i stop the audio before playing it again? 如何在再次播放之前停止音频?

您为什么不简单地使用AVAudioPlayer

The SoundEffect class is a wrapper around the C-based System Sounds API (see the .m file from the Bubble Level project), which is a simple "fire and forget" style API that doesn't provide a "stop" function. SoundEffect类是基于C的系统声音API的包装(请参见Bubble Level项目的.m文件 ),这是一种简单的“即发即弃”样式的API,不提供“停止”功能。 More info in the System Sounds Services Reference . 有关更多信息,请参见《 系统声音服务参考》

I also agree with (and have voted up) zoul's suggestion to use AVAudioPlayer . 我也同意(并已投票赞成)zoul的使用AVAudioPlayer的建议。 System Sounds are wholly inappropriate for long, encoded audio files like songs in MP3 files. 系统声音完全不适用于长时间编码的音频文件,例如MP3文件中的歌曲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM