sqlite存mp3

前端之家收集整理的这篇文章主要介绍了sqlite存mp3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_403_1@

Step-1: 首先將.mp3檔案放入Project/res/raw/裡,如下:

@H_403_1@

sqlite存mp3 - On<wbr></p>ce - 许兴旺的博客

@H_403_1@ 程式一開始執行,建立一個資料庫,含有BLOB欄位,如下之指令:

@H_403_1@ sql = "create table mySong("

@H_403_1@ + "song_no text not null,"

@H_403_1@ + "song_mp3 blob );";

@H_403_1@ try {

@H_403_1@ db.execsql(sql);

@H_403_1@ } catch (sqlException e) {

@H_403_1@ Log.e("ERROR",e.toString());

@H_403_1@ return;

@H_403_1@ }

Step-2: Project讀取*.mp3歌曲,然後分段儲存到sqliteBLOB裡,如下之指令:

@H_403_1@ InputStream is = getResources().openRawResource(rid);

@H_403_1@ int bufSize = 63*1024;

@H_403_1@ byte[] buffer@H_403_1@ = new byte[bufSize];

@H_403_1@ try {

@H_403_1@@H_403_1@ int size = is.read(buffer);

@H_403_1@@H_403_1@ while(size >= 0){

@H_403_1@ ByteArrayOutputStream out = new ByteArrayOutputStream(size);

@H_403_1@@H_403_1@ @H_403_1@ out.write(buffer,size);

@H_403_1@@H_403_1@ @H_403_1@ out.flush();

@H_403_1@ @H_403_1@out.close();

@H_403_1@ cv.put("song_mp3",out.toByteArray());

@H_403_1@ db.insert("mySong",null,cv);

@H_403_1@ size = is.read(buffer);

@H_403_1@@H_403_1@ @H_403_1@ }

@H_403_1@@H_403_1@ } catch (IOException e) {

@H_403_1@@H_403_1@ @H_403_1@Log.e("ERROR",e.toString());

@H_403_1@@H_403_1@ }

Step-3: sqliteBLOB裡,讀取歌曲並存入

/data/data/com.misoo.SQ01/files/song.mp3

如下之指令

@H_404_411@@H_403_1@@H_403_1@FileOutputStream os = null;

@H_403_1@ try{

@H_403_1@@H_403_1@ @H_403_1@os = openFileOutput("song.mp3",MODE_WORLD_READABLE);

@H_403_1@ } catch(FileNotFoundException e){

@H_403_1@@H_403_1@ @H_403_1@Log.e("ERROR",e.toString());

@H_403_1@ }

@H_403_1@ byte[] red_buf; @H_403_1@ @H_403_1@

@H_403_1@ //----------------------------------------

@H_403_1@ mOpenHelper = new DatabaseHelper(this);

@H_403_1@ sqliteDatabase db = mOpenHelper.getReadableDatabase();

@H_403_1@ String col[] = {"song_no","song_mp3" };

@H_403_1@ cur = db.query("mySong",col,cond,null);

@H_403_1@ int k =0;

@H_403_1@ cur.moveToFirst();

@H_403_1@ try{

@H_403_1@ @H_403_1@ while(!cur.isAfterLast()){

@H_403_1@ @H_403_1@ @H_403_1@ @H_403_1@red_buf = cur.getBlob(1);

@H_403_1@ @H_403_1@ @H_403_1@ @H_403_1@os.write(red_buf);

@H_403_1@ @H_403_1@k++;

@H_403_1@ @H_403_1@ @H_403_1@ @H_403_1@cur.moveToNext();

@H_403_1@ @H_403_1@}

@H_403_1@ @H_403_1@os.flush();

@H_403_1@ @H_403_1@os.close();

@H_403_1@ @H_403_1@}catch(Exception e){

@H_403_1@ @H_403_1@Log.e("ERROR",e.toString());

@H_403_1@ @H_403_1@ return;

@H_403_1@ @H_403_1@}

Step-4: 使用MediaPlayer

@H_403_1@ /data/data/com.misoo.SQ01/files/song.mp3

@H_403_1@ 播放出來,如下之指令

@H_403_1@ String path = "/data/data/com.misoo.SQ01/files/song.mp3";

@H_403_1@ mPlayer = new MediaPlayer();

@H_403_1@ try {

@H_403_1@ mPlayer.setDataSource(path);

@H_403_1@ mPlayer.prepare();

@H_403_1@ } catch (IOException e) {

@H_403_1@ e.printStackTrace();

@H_403_1@ }

@H_403_1@ mPlayer.start();

}

@H_403_1@

其實,BLOB欄位可儲存很大的資料量,在本範例裡,刻意將歌曲切成許多段,逐一存入資料庫裏。其目的只是為了舉例而已。

package com.misoo.SQ01;

import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import android.app.Activity;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlException;

import android.database.sqlite.sqliteDatabase;

import android.database.sqlite.sqliteOpenHelper;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

public class ac01 extends Activity implements OnClickListener{

@H_403_1@ @H_403_1@ private static final String DB_NAME = "mp3Song.db";

@H_403_1@ private static final int DB_VERSION = 2;

@H_403_1@ private Button btn,btn2,btn3;

@H_403_1@ private Cursor cur;

@H_403_1@ private MediaPlayer mPlayer;

@H_403_1@ private static class DatabaseHelper extends sqliteOpenHelper {

@H_403_1@ DatabaseHelper(Context context) {

@H_403_1@ super(context,DB_NAME,DB_VERSION);

@H_403_1@ }

@H_403_1@ @Override

@H_403_1@ public void onCreate(sqliteDatabase db) {

@H_403_1@ }

@H_403_1@ @Override

@H_403_1@ public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {

@H_403_1@ }

@H_403_1@ }

@H_403_1@ @H_403_1@

@H_403_1@ @Override

@H_403_1@ public void onCreate(Bundle icicle) {

@H_403_1@ super.onCreate(icicle);

@H_403_1@ @H_403_1@ LinearLayout layout = new LinearLayout(this);

@H_403_1@ layout.setOrientation(LinearLayout.VERTICAL);

@H_403_1@ btn = new Button(this);

@H_403_1@ btn.setId(101);

@H_403_1@ btn.setText("play");

@H_403_1@ btn.setBackgroundResource(R.drawable.heart);

@H_403_1@ btn.setOnClickListener(this);

@H_403_1@ LinearLayout.LayoutParams param

@H_403_1@ @H_403_1@ = new LinearLayout.LayoutParams(80,50);

@H_403_1@ param.topMargin = 10;

@H_403_1@ layout.addView(btn,param);

@H_403_1@ btn2 = new Button(this);

@H_403_1@ btn2.setId(102);

@H_403_1@ btn2.setText("stop");

@H_403_1@ btn2.setBackgroundResource(R.drawable.heart);

@H_403_1@ btn2.setOnClickListener(this);

@H_403_1@ layout.addView(btn2,param);

@H_403_1@ btn3 = new Button(this);

@H_403_1@ btn3.setId(103);

@H_403_1@ btn3.setText("exit");

@H_403_1@ btn3.setBackgroundResource(R.drawable.heart);

@H_403_1@ btn3.setOnClickListener(this);

@H_403_1@ layout.addView(btn3,param);

@H_403_1@ setContentView(layout);

@H_403_1@ setTitle("Saving into sqliteDB...");

@H_403_1@ //---------------------------------

@H_403_1@ init();

@H_403_1@ setTitle("Saved in sqliteDB.");

@H_403_1@ }

@H_403_1@ private DatabaseHelper mOpenHelper;

@H_403_1@ public void init(){

@H_403_1@ @H_403_1@ @H_403_1@mOpenHelper = new DatabaseHelper(this);

@H_403_1@@H_403_1@ sqliteDatabase db = mOpenHelper.getWritableDatabase();

@H_403_1@ //-----------------------------------

@H_403_1@ String sql = "drop table mySong";

@H_403_1@ try {

@H_403_1@ db.execsql(sql);

@H_403_1@ } catch (sqlException e) {

@H_403_1@ Log.e("ERROR",e.toString());

@H_403_1@ }

@H_403_1@ //-----------------------------------

@H_403_1@ @H_403_1@ @H_403_1@sql = "create table mySong("

@H_403_1@ + "song_no text not null,"

@H_403_1@ + "song_mp3 blob );";

@H_403_1@ try {

@H_403_1@ db.execsql(sql);

@H_403_1@ } catch (sqlException e) {

@H_403_1@ Log.e("ERROR",e.toString());

@H_403_1@ return;

@H_403_1@ }

@H_403_1@ //---------------------------------

@H_403_1@ SaveOneSong(db,"s01",R.raw.den_li_guing);

@H_403_1@ }@H_403_1@

@H_403_1@ public void SaveOneSong(sqliteDatabase db,String key,int rid){

@H_403_1@ @H_403_1@ @H_403_1@ContentValues cv = new ContentValues();

@H_403_1@ @H_403_1@ @H_403_1@cv.put("song_no",key);

@H_403_1@

@H_403_1@ InputStream is = getResources().openRawResource(rid);

@H_403_1@ int bufSize = 63*1024;

@H_403_1@ byte[] buffer@H_403_1@ = new byte[bufSize];

@H_403_1@ try {

@H_403_1@@H_403_1@ int size = is.read(buffer);

@H_403_1@@H_403_1@ while(size >= 0){

@H_403_1@@H_403_1@ ByteArrayOutputStream out = new ByteArrayOutputStream(size);

@H_403_1@@H_403_1@ @H_403_1@ out.write(buffer,size);

@H_403_1@@H_403_1@ @H_403_1@ out.flush();

@H_403_1@ out.close();

@H_403_1@ cv.put("song_mp3",out.toByteArray());

@H_403_1@ db.insert("mySong",cv);

@H_403_1@ size = is.read(buffer);

@H_403_1@@H_403_1@ }

@H_403_1@@H_403_1@ } catch (IOException e) {

@H_403_1@@H_403_1@ @H_403_1@Log.e("ERROR",e.toString());

@H_403_1@@H_403_1@ }

@H_403_1@ }@H_403_1@

@H_403_1@

public void play(String cond){

@H_403_1@ @H_403_1@FileOutputStream os = null;

@H_403_1@ @H_403_1@try{

@H_403_1@ @H_403_1@os = openFileOutput("song.mp3",MODE_WORLD_READABLE);

@H_403_1@ @H_403_1@} catch(FileNotFoundException e){

@H_403_1@ @H_403_1@Log.e("ERROR",e.toString());

@H_403_1@ @H_403_1@}

@H_403_1@ @H_403_1@ byte[] red_buf; @H_403_1@ @H_403_1@

@H_403_1@ @H_403_1@//----------------------------------------

@H_403_1@ @H_403_1@mOpenHelper = new DatabaseHelper(this);

@H_403_1@ sqliteDatabase db = mOpenHelper.getReadableDatabase();

@H_403_1@ @H_403_1@String col[] = {"song_no","song_mp3" };

@H_403_1@ @H_403_1@cur = db.query("mySong",null);

@H_403_1@ @H_403_1@int k =0;

@H_403_1@ @H_403_1@cur.moveToFirst();

@H_403_1@ @H_403_1@try{

@H_403_1@ @H_403_1@ while(!cur.isAfterLast()){

@H_403_1@ @H_403_1@ @H_403_1@ @H_403_1@red_buf = cur.getBlob(1);

@H_403_1@ @H_403_1@ @H_403_1@ @H_403_1@os.write(red_buf);

@H_403_1@ @H_403_1@k++;

@H_403_1@ @H_403_1@ @H_403_1@ @H_403_1@cur.moveToNext();

@H_403_1@ @H_403_1@ @H_403_1@ @H_403_1@}

@H_403_1@ @H_403_1@ os.flush();

@H_403_1@ @H_403_1@ os.close();

@H_403_1@

@H_403_1@ @H_403_1@}catch(Exception e){

@H_403_1@ @H_403_1@Log.e("ERROR",e.toString());

@H_403_1@ @H_403_1@return;

@H_403_1@ @H_403_1@}

@H_403_1@ String path = "/data/data/com.misoo.SQ01/files/song.mp3";

@H_403_1@ mPlayer = new MediaPlayer();

@H_403_1@ try {

@H_403_1@ mPlayer.setDataSource(path);

@H_403_1@ mPlayer.prepare();

@H_403_1@ } catch (IOException e) {

@H_403_1@ e.printStackTrace();

@H_403_1@ }

@H_403_1@ mPlayer.start();

@H_403_1@ }

@H_403_1@ public void onClick(View v) {

@H_403_1@ switch (v.getId()) {

@H_403_1@ case 101:

@H_403_1@ String cond = "song_no='s01'";

@H_403_1@ play(cond);

@H_403_1@ break;

@H_403_1@ case 102:

@H_403_1@ stop();

@H_403_1@ @H_403_1@ break;

@H_403_1@ case 103:

@H_403_1@ stop();

@H_403_1@ finish();

@H_403_1@ break;

@H_403_1@ }

@H_403_1@ }

@H_403_1@ public void stop() {

@H_403_1@ @H_403_1@ if (mPlayer != null) {

@H_403_1@ @H_403_1@ @H_403_1@ mPlayer.stop();

@H_403_1@ mPlayer.release();

@H_403_1@ mPlayer = null;

@H_403_1@ }

@H_403_1@ }

}

猜你在找的Sqlite相关文章