sqlite存mp3

sqlite存mp3,第1张

概述Step-1: 首先將.mp3檔案放入Project的/res/raw/裡,如下:                   程式一開始執行,建立一個資料庫,含有BLOB欄位,如下之指令:        sql = "create table mySong("             + "song_no text not null, "             + "song_mp3 blob );"

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

ce - 许兴旺的博客" src="http://www.android1.net/upload/2009215684363.jpg">

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

sql = "create table mySong("

+ "song_no text not null,"

+ "song_mp3 blob );";

try {

db.execsql(sql);

} catch (sqlException e) {

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

return;

}

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

inputStream is = getResources().openRawResource(rID);

int bufSize = 63*1024;

byte[] buffer = new byte[bufSize];

try {

int size = is.read(buffer);

while(size >= 0){

ByteArrayOutputStream out = new ByteArrayOutputStream(size);

out.write(buffer,size);

out.flush();

out.close();

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

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

size = is.read(buffer);

}

} catch (IOException e) {

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

}

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

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

如下之指令

fileOutputStream os = null;

try{

os = openfileOutput("song.mp3",MODE_WORLD_READABLE);

} catch(fileNotFoundException e){

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

}

byte[] red_buf;

//----------------------------------------

mOpenHelper = new DatabaseHelper(this);

sqliteDatabase db = mOpenHelper.getReadableDatabase();

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

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

int k =0;

cur.movetoFirst();

try{

while(!cur.isAfterLast()){

red_buf = cur.getBlob(1);

os.write(red_buf);

k++;

cur.movetoNext();

}

os.flush();

os.close();

}catch(Exception e){

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

return;

}

Step-4: 使用MediaPlayer

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

播放出來,如下之指令

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

mPlayer = new MediaPlayer();

try {

mPlayer.setDataSource(path);

mPlayer.prepare();

} catch (IOException e) {

e.printstacktrace();

}

mPlayer.start();

}

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

@H_419_914@package com.misoo.SQ01;

@H_419_914@import java.io.ByteArrayOutputStream;

@H_419_914@import java.io.fileNotFoundException;

@H_419_914@import java.io.fileOutputStream;

@H_419_914@import java.io.IOException;

@H_419_914@import java.io.inputStream;

@H_419_914@

@H_419_914@import androID.app.Activity;

@H_419_914@import androID.content.ContentValues;

@H_419_914@import androID.content.Context;

@H_419_914@import androID.database.Cursor;

@H_419_914@import androID.database.sqlException;

@H_419_914@import androID.database.sqlite.sqliteDatabase;

@H_419_914@import androID.database.sqlite.sqliteOpenHelper;

@H_419_914@import androID.media.MediaPlayer;

@H_419_914@import androID.os.Bundle;

@H_419_914@import androID.util.Log;

@H_419_914@import androID.vIEw.VIEw;

@H_419_914@import androID.vIEw.VIEw.OnClickListener;

@H_419_914@import androID.Widget.button;

@H_419_914@import androID.Widget.linearLayout;

@H_419_914@

@H_419_914@public class ac01 extends Activity implements OnClickListener{

@H_419_914@ private static final String DB_name = "mp3Song.db";

@H_419_914@ private static final int DB_VERSION = 2;

@H_419_914@ private button btn,btn2,btn3;

@H_419_914@ private Cursor cur;

@H_419_914@ private MediaPlayer mPlayer;

@H_419_914@

@H_419_914@ private static class DatabaseHelper extends sqliteOpenHelper {

@H_419_914@ DatabaseHelper(Context context) {

@H_419_914@ super(context,DB_name,DB_VERSION);

@H_419_914@ }

@H_419_914@ @OverrIDe

@H_419_914@ public voID onCreate(sqliteDatabase db) {

@H_419_914@ }

@H_419_914@ @OverrIDe

@H_419_914@ public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {

@H_419_914@ }

@H_419_914@ }

@H_419_914@

@H_419_914@ @OverrIDe

@H_419_914@ public voID onCreate(Bundle icicle) {

@H_419_914@ super.onCreate(icicle);

@H_419_914@ linearLayout layout = new linearLayout(this);

@H_419_914@ layout.setorIEntation(linearLayout.VERTICAL);

@H_419_914@

@H_419_914@ btn = new button(this);

@H_419_914@ btn.setID(101);

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

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

@H_419_914@ btn.setonClickListener(this);

@H_419_914@ linearLayout.LayoutParams param

@H_419_914@ = new linearLayout.LayoutParams(80,50);

@H_419_914@ param.topmargin = 10;

@H_419_914@ layout.addVIEw(btn,param);

@H_419_914@

@H_419_914@ btn2 = new button(this);

@H_419_914@ btn2.setID(102);

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

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

@H_419_914@ btn2.setonClickListener(this);

@H_419_914@ layout.addVIEw(btn2,param);

@H_419_914@

@H_419_914@ btn3 = new button(this);

@H_419_914@ btn3.setID(103);

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

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

@H_419_914@ btn3.setonClickListener(this);

@H_419_914@ layout.addVIEw(btn3,param);

@H_419_914@ setContentVIEw(layout);

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

@H_419_914@ //---------------------------------

@H_419_914@ init();

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

@H_419_914@ }

@H_419_914@ private DatabaseHelper mOpenHelper;

@H_419_914@ public voID init(){

@H_419_914@ mOpenHelper = new DatabaseHelper(this);

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

@H_419_914@ //-----------------------------------

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

@H_419_914@ try {

@H_419_914@ db.@H_403_1924@execsql(sql);

@H_419_914@ } catch (@H_403_1924@sqlException e) {

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

@H_419_914@ }

@H_419_914@ //-----------------------------------

@H_419_914@ sql = "create table mySong("

@H_419_914@ + "song_no text not null,"

@H_419_914@ + "song_mp3 blob );";

@H_419_914@ try {

@H_419_914@ db.execsql(sql);

@H_419_914@ } catch (sqlException e) {

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

@H_419_914@ return;

@H_419_914@ }

@H_419_914@ //---------------------------------

@H_419_914@ SaveOnesong(db,"s01",R.raw.den_li_guing);

@H_419_914@ }

@H_419_914@ public voID SaveOnesong(sqliteDatabase db,String key,int rID){

@H_419_914@ ContentValues cv = new ContentValues();

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

@H_419_914@

@H_419_914@ inputStream is = getResources().openRawResource(rID);

@H_419_914@ int bufSize = 63*1024;

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

@H_419_914@ try {

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

@H_419_914@ while(size >= 0){

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

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

@H_419_914@ out.flush();

@H_419_914@ out.close();

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

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

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

@H_419_914@ }

@H_419_914@ } catch (IOException e) {

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

@H_419_914@ }

@H_419_914@ }

@H_419_914@

@H_419_914@public voID play(String cond){

@H_419_914@ fileOutputStream os = null;

@H_419_914@ try{

@H_419_914@ os = openfileOutput("song.mp3",MODE_WORLD_READABLE);

@H_419_914@ } catch(fileNotFoundException e){

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

@H_419_914@ }

@H_419_914@ byte[] red_buf;

@H_419_914@ //----------------------------------------

@H_419_914@ mOpenHelper = new DatabaseHelper(this);

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

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

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

@H_419_914@ int k =0;

@H_419_914@ cur.movetoFirst();

@H_419_914@ try{

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

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

@H_419_914@ os.write(red_buf);

@H_419_914@ k++;

@H_419_914@ cur.movetoNext();

@H_419_914@ }

@H_419_914@ os.flush();

@H_419_914@ os.close();

@H_419_914@

@H_419_914@ }catch(Exception e){

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

@H_419_914@ return;

@H_419_914@ }

@H_419_914@

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

@H_419_914@ mPlayer = new MediaPlayer();

@H_419_914@ try {

@H_419_914@ mPlayer.setDataSource(path);

@H_419_914@ mPlayer.prepare();

@H_419_914@ } catch (IOException e) {

@H_419_914@ e.printstacktrace();

@H_419_914@ }

@H_419_914@ mPlayer.start();

@H_419_914@ }

@H_419_914@ public voID onClick(VIEw v) {

@H_419_914@ switch (v.getID()) {

@H_419_914@ case 101:

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

@H_419_914@ play(cond);

@H_419_914@ break;

@H_419_914@ case 102:

@H_419_914@ stop();

@H_419_914@ break;

@H_419_914@ case 103:

@H_419_914@ stop();

@H_419_914@ finish();

@H_419_914@ break;

@H_419_914@ }

@H_419_914@ }

@H_419_914@ public voID stop() {

@H_419_914@ if (mPlayer != null) {

@H_419_914@ mPlayer.stop();

@H_419_914@ mPlayer.release();

@H_419_914@ mPlayer = null;

@H_419_914@ }

@H_419_914@ }

@H_419_914@}

总结

以上是内存溢出为你收集整理的sqlite存mp3全部内容,希望文章能够帮你解决sqlite存mp3所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1177825.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存