1. 程式人生 > >ios- 音樂播放器(1)

ios- 音樂播放器(1)

音樂播放器

這裡做一個簡單的音樂播放器,資料是載入的本地的 實現了簡單的點選播放的功能 

使用了#import <AVFoundation/AVFoundation.h> 框架  之後會持續的完善更新音樂播放器

核心程式碼:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //播放音樂
    
    QHMusic *music = self.musicArray[indexPath.row];
    
    //建立播放器
    
    NSURL *url = [[NSBundle mainBundle]URLForResource:music.filename withExtension:nil];
    
    //注意這裡必須使用全域性變數
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    
    //緩衝(以便後面播放比較流暢)
    
    [audioPlayer prepareToPlay];
    
    //開始播放
    [audioPlayer play];
    
    self.audioPlayer = audioPlayer;
    
    
}

主要程式碼

資料模型

#import <Foundation/Foundation.h>

@interface QHMusic : NSObject

@property(nonatomic,copy)NSString *name;

@property(nonatomic,copy)NSString *filename;

@property(nonatomic,copy)NSString *singer;

@property(nonatomic,copy)NSString *singerIcon;

@property(nonatomic,copy)NSString *icon;

+(id)musicWithDict:(NSDictionary *)dict;

-(id)initWithDict:(NSDictionary *)dict;


@end
#import "QHMusicViewController.h"
#import "QHMusic.h"
#import <AVFoundation/AVFoundation.h>


@interface QHMusicViewController ()

@property(nonatomic,strong)NSArray *musicArray;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)AVAudioPlayer *audioPlayer;
@end

@implementation QHMusicViewController

-(NSArray *)musicArray
{
    if (!_musicArray) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"Musics.plist" ofType:nil];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *objs = [NSMutableArray array];
        
        for(NSDictionary * dict in array)
        {
            QHMusic *music = [QHMusic musicWithDict:dict];
            [objs addObject:music];
        }
        _musicArray = objs;
    }
    return _musicArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.musicArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QHMusic *music = self.musicArray[indexPath.row];
    static NSString *cellName = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
    }
    
    cell.imageView.image = [UIImage imageNamed:music.singerIcon];
    cell.textLabel.text = music.name;
    cell.detailTextLabel.text = music.singer;
    
    NSLog(@"%@",music.singer);
    
    NSLog(@"%@",cell.detailTextLabel.text);
    // Configure the cell...
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //播放音樂
    
    QHMusic *music = self.musicArray[indexPath.row];
    
    //建立播放器
    
    NSURL *url = [[NSBundle mainBundle]URLForResource:music.filename withExtension:nil];
    
    //注意這裡必須使用全域性變數
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    
    //緩衝(以便後面播放比較流暢)
    
    [audioPlayer prepareToPlay];
    
    //開始播放
    [audioPlayer play];
    
    self.audioPlayer = audioPlayer;
    
    
}
@end


#import "QHMusic.h"

@implementation QHMusic

+(id)musicWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}

-(id)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}


@end


相關推薦

ios- 音樂播放1

音樂播放器 這裡做一個簡單的音樂播放器,資料是載入的本地的 實現了簡單的點選播放的功能  使用了#import <AVFoundation/AVFoundation.h> 框架  之後會持續的完善更新音樂播放器 核心程式碼: -(void)tableView

Android實現音樂播放

simple ani call ket 打開文件 界面 方式 .cn 點擊 Graphical User Interface 本篇文章記錄了我實現Android簡單音樂播放器的過程,(一)中介紹了怎麽構建音樂播放器的前端頁面。首先大家看一下,界面最後是這樣的(界面有

QT5:C++實現基於Multimedia的音樂播放

播放列表 nbsp eight 自帶 讀取 set 技術 strong ati 前段時間C++課設,決定做個播放器,於是參考了網上的代碼後,做了個很簡陋的音樂播放器(只寫了MP3格式)出來,雖然功能甚少,但還是決定把過程記錄一下。 成品如下圖: 播放器功能: 上、下一首

QT5:C++實現基於multimedia的音樂播放

彈框 rem tooltip loop dialog ets posit list sch 今天接著上一篇來實現播放器的槽函數。 先來實現播放模式,槽函數如下: 1 //播放模式 2 void Music::musicPlayPattern() 3 { 4

用Vue來實現音樂播放:自動輪播圖啊

-s AR better hold ons ntp next start upd slider.vue組件的模板部分 <template> <div class="slider" ref="slider"> <div class=

Vue實現音樂播放:輪播圖組件

item [] tin neo pic () client link ons 輪播圖組件 <template> <div class="slider" ref="slider"> <div class="slider-

用Vue來實現音樂播放:歌單數據接口分析

QQ 插件 但是 之間 nbsp 跨域問題 前端 代理服務 一點 z這裏如果我們和之前獲取輪播圖的數據一樣來獲取表單的數據 發現根本獲取不到 原因是qq音樂在請求頭裏面加了authority和refer等 但是如果我們通過jsonp實現跨域

基於QT的網路音樂播放

自學Qt已經有一段時間了,但是始終感覺自己還是很弱(其實並不是感覺自己很弱,是自己本來就很弱,哈哈)。自己也照著書上敲了幾個例子,但覺得還是要寫點東西才能真正運用起來。所以,前段時間就寫了個很簡單的音樂播放器。在這裡總結一下,寫得不好,所以請各位不要介意。 先看

基於QT的網路音樂播放

得到AlbumID和FileHash後訪問http://www.kugou.com/yy/index.php?r=play/getdata&hash= &album_id= &_=1497972864535 其中hash後面跟的就是前面

基於QT的網路音樂播放

關於歌詞的顯示,其實我的主要思想就是解析歌詞部分的字串。歌詞顯示分為兩部分,一部分是播放器右側的歌詞顯示以及下面的桌面歌詞的顯示。其中桌面歌詞讓我很難受,想了很久,後面看到一個大佬的一篇文章後才有了思路。 先看效果圖: 其實就是解析lrc歌詞字串。lrc歌詞分

Android AIDL技術實戰專案-音樂播放

實現功能 使用AIDL服務實現: 音樂播放、暫停、上一曲、下一曲、歌詞功能 音樂播放模式:順序播放 、隨機播放、單曲播放 多個頁面呼叫AIDL服務 主頁面底部音樂迷你控制器 音樂詳情頁面 程式

個人專案——音樂播放

HomeActivity的程式碼: package com.wwj.sb.activity; import java.io.File; import java.util.ArrayList; import java.util.List; import android

Android AIDL技術實戰專案-音樂播放-使用retrofit完成音樂API的封裝

使用retrofit完成音樂API的封裝 程式碼結構 1、匯入retrofit相關庫 compile 'com.squareup.retrofit2:retrofit:2.2.0' 2、編寫介面API public interfac

Android 超簡單音樂播放歌詞的實現

關於歌詞 有下面這些: 歌詞的獲取 歌詞的解析 自定義View 歌詞的獲取 歌詞的獲取分為兩種,一種是從本地一種是通過網路上提供的API獲取。我選擇的是歌詞迷的API http://api.geci.me/en/latest/ 說實話,這

手把手教你做音樂播放音樂列表的儲存

5.2 MusicService的配合 MusicService對外提供新增播放列表的介面,對內要管理PlayListContentProvider。它提供了下面的介面, addPlayList():新增播放列表。這裡新增列表應該有兩種形式,一種是一次性新

JAVA音樂播放實現列表點選播放

package kk; import java.applet.Applet; import java.applet.AudioClip; import java.awt.BorderLayout; import java.awt.EventQueue; import j

Android 超簡單音樂播放拖動歌曲進度條~音量控制~

音量控制 通過接收廣播進行改變 設定全域性變數 private AudioManager mAudioManager; private ContentObserver mVoiceObserver; private MyVolum

Android 超簡單音樂播放搜尋網路歌曲,獲得熱門榜單GridView易源api的使用JSON的解析重新整理

首先感謝易源API! 提供了QQ音樂的介面~ 咳咳... 明天要寫文件..看來..哎..我的歌詞要等假期了? 迴歸正題~ 首先上一下我的介面啊實現啊啥的~ 點選榜單會出現對應的歌曲~ 可以搜尋網路歌曲~ 可以重新整理~ 先寫榜單的實現~ 首先是net這個Frag

用Vue來實現音樂播放十六:滾動列表的實現

com 作用 efault nor 大小 -s stylus BE ack 滾動列表是一個基礎組件 他是基於scroll組件實現的 在base文件夾下面創建一個list-view文件夾 裏面有list-view.vue組件 <template>

用Vue來實現音樂播放十八:右側快速入口點擊高亮

為我 UC 沒有 short cut this 必須 左右 png 問題一:當我們點擊右側快速入口的時候 被點擊的地方高亮 首先我們要知道右側快速入口是為什麽高亮??因為當watch()監控到scrollY的變化了的時候 將scrollY的值和listHeight相比較